// Copyright (C) 2009 by NHN Corporation. All rights reserved.
// Released under the terms of the GNU General Public License version 2 or later.
package fitnesse.wikitext.widgets;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import fitnesse.components.Base64;

public class ChartWidget extends ParentWidget {
	public static final String REGEXP = "^!chart-(file|data|url) [^\r\n]*";
	private static final Pattern pattern = Pattern.compile("^!chart-(file|data|url) ([^\r\n]*)");

	private String ind;
	private String data;

	public ChartWidget(ParentWidget parent, String text) {
		super(parent);
		Matcher match = pattern.matcher(text);

		if (match.find()) {
			this.ind = match.group(1);
			this.data = match.group(2);
		}
	}

	public String render() throws Exception {
		if (ind == null || data == null) {
			return "";
		}

		return "<img src=\"/chart?" + ind + "=" + Base64.encode(data) + "\">";
	}
}
